import $ from 'jquery';

const ModuleUser = {
  state: {
    id: "",
    username: "",
    photo: "",
    followerCount: 0,
    followedUser: []
  },
  getters: {
  },
  mutations: {
      updateUser(state, user) {
          state.id = user.id;
          state.username = user.username;
          state.photo = user.photo;
          state.followerCount = user.followerCount;
          state.followedUser = user.followedUser;
      },
      logout() {
          localStorage.clear();
          location.reload();
      }
  },
  actions: {
      login(context, data) {
        $.ajax({
            url: "/api/userlist",
            type: "get",
            data: {
                username: data.username,
                password: data.password,
            },
            success(resp) {
                localStorage.setItem('login_user_id', resp[0].id);
                localStorage.setItem('login_user_followerCount', resp[0].followerCount);
                localStorage.setItem('login_user_photo', resp[0].photo);
                localStorage.setItem('login_user_username', resp[0].username);
                localStorage.setItem('login_user_followedUser', resp[0].followedUser);
                data.success();
            },
            error() {
                data.error();
            }
        });
      },
  },
  modules: {
  }
};

export default ModuleUser;
